package com.tzinfo.tzt.activity.util;

import android.app.Application;
import android.content.Context;
import android.location.Location;
import android.widget.Toast;

import com.baidu.frontia.FrontiaApplication;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.map.MKEvent;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.utils.StorageUtils;
import com.tzinfo.tzt.R;

import java.io.File;

public class MyApplication extends Application {
	
	private static boolean isProgramExit = false;
	private static MyApplication mInstance = null;
    public boolean m_bKeyRight = true;
    public static Location location;
    public static int nowLocationCode=-1;
    public static int locationCode=-1;
    public static double latitude = 0;
	public static double longitude = 0;
    
    public static final String strKey = "3d6e0a636d0bb20dad2051e263291cb0";
	
    
    //手机分辨率
    public static int widthPixels=-1;
    public static int heightPixels=-1;
	public void onCreate() {
        FrontiaApplication.initFrontiaApplication(this);
		super.onCreate();
		mInstance = this;
//		DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
//				.cacheInMemory().cacheOnDisc()
//				.showImageOnFail(R.drawable.ic_empty)
//				.showStubImage(R.drawable.ic_empty).build();
//		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
//				getApplicationContext())
//				.threadPriority(Thread.NORM_PRIORITY - 2)
//				.tasksProcessingOrder(QueueProcessingType.LIFO)
//				.defaultDisplayImageOptions(defaultOptions)
//				.denyCacheImageMultipleSizesInMemory()
//				.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
//				.memoryCacheSize(2 * 1024 * 1024)
//				.discCacheFileNameGenerator(new Md5FileNameGenerator())
//				.discCacheSize(50 * 1024 * 1024).discCacheFileCount(100)
//				.build();
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_stub) // resource or drawable
                .showImageForEmptyUri(R.drawable.ic_empty) // resource or drawable
                .showImageOnFail(R.drawable.ic_error) // resource or drawable
                .delayBeforeLoading(0)
                .cacheInMemory(true) // default
                .cacheOnDisk(true) // default
                .build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
                .diskCacheExtraOptions(480, 800, null)
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
                .memoryCacheSize(2 * 1024 * 1024)
                .diskCacheSize(50 * 1024 * 1024)
                .diskCacheFileCount(100)
                .build();
  		ImageLoader.getInstance().init(config);
//		initEngineManager(this);
	}


//	@Override
	//建议在您app的退出之前调用mapadpi的destroy()函数，避免重复初始化带来的时间消耗
//	public void onTerminate() {
//		// TODO Auto-generated method stub
//	    if (mBMapManager != null) {
//            mBMapManager.destroy();
//            mBMapManager = null;
//        }
//		super.onTerminate();
//	}
	
	
//	public void initEngineManager(Context context) {
//        if (mBMapManager == null) {
//            mBMapManager = new BMapManager(context);
//        }
//        if (!mBMapManager.init(strKey,new MyGeneralListener())) {
//            Toast.makeText(MyApplication.getInstance().getApplicationContext(), 
//                    "BMapManager  初始化错误!", Toast.LENGTH_LONG).show();
//        }
//	}
	
	// 常用事件监听，用来处理通常的网络错误，授权验证错误等
   public static class MyGeneralListener implements MKGeneralListener {
        
        @Override
        public void onGetNetworkState(int iError) {
            if (iError == MKEvent.ERROR_NETWORK_CONNECT) {
                Toast.makeText(MyApplication.getInstance().getApplicationContext(), "您的网络出错啦！",
                    Toast.LENGTH_LONG).show();
            }
            else if (iError == MKEvent.ERROR_NETWORK_DATA) {
                Toast.makeText(MyApplication.getInstance().getApplicationContext(), "输入正确的检索条件！",
                        Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onGetPermissionState(int iError) {
            if (iError ==  MKEvent.ERROR_PERMISSION_DENIED) {
                //授权Key错误：
                Toast.makeText(MyApplication.getInstance().getApplicationContext(), 
                        "请在 MyApplication.java文件输入正确的授权Key！", Toast.LENGTH_LONG).show();
                MyApplication.getInstance().m_bKeyRight = false;
            }
        }
    }
	
	public static MyApplication getInstance() {
		return mInstance;
	}
	
	
	public void setExit(boolean exit) {
		isProgramExit = exit;
	}

	public boolean isExit() {
		return isProgramExit;
	}




	public static boolean isProgramExit() {
		return isProgramExit;
	}


	public static void setProgramExit(boolean isProgramExit) {
		MyApplication.isProgramExit = isProgramExit;
	}
	
}